package com.flick.baseCtrl;

import com.flick.baseService.BaseServ;
import com.flick.common.Filters;
import com.flick.common.PageParam;
import com.flick.common.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * Created by flick on 2017/4/23.
 */
@EnableAutoConfiguration
@ResponseBody
@CrossOrigin
public class BaseCtrl <T extends BaseServ>{

    @Autowired
    protected T serv;

    /**
     * 查看所有商品类型列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list(){
        return serv.list();
    }

    /**
     * 查看商品分类列表（带分页）
     * @param rows 页码
     * @param page 每页大小
     * @return
     */
    @RequestMapping(params = {"page","rows"},method = RequestMethod.GET)
    public Object page(@RequestParam Integer page,@RequestParam Integer rows,
                       @RequestParam(required = false) String sidx,@RequestParam(required = false) String sord,
                       @RequestParam(required = false) String searchField,@RequestParam(required = false) String searchString,
                       @RequestParam(required = false) String searchOper){
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(page);
        pageParam.setPageSize(rows);
        pageParam.setSidx(sidx);
        pageParam.setSord(sord);
        Filters filters = new Filters();
        filters.setGroupOp("and");
        ArrayList<Rule> rules = new ArrayList<>();
        Rule rule = new Rule();
        rule.setField(searchField);
        rule.setData(searchString);
        rule.setOp(searchOper);
        rules.add(rule);
        filters.setRules(rules);
        pageParam.setFilters(filters);
        return serv.page(pageParam);
    }

    /**
     * 根据ID查看商品分类
     * @param id 商品ID
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Object get(@PathVariable String id){
        return serv.get(id);
    }
}
